package com.cr.business.controller;

import com.cr.business.domain.TbUserBaseInfo;
import com.cr.business.domain.dto.UserReqDto;
import com.cr.business.service.ITbUserBaseInfoService;
import com.cr.common.annotation.Log;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.core.page.TableDataInfo;
import com.cr.common.enums.BusinessType;
import com.cr.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户基本信息Controller
 * 
 * @author tang
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/user/info")
public class TbUserBaseInfoController extends BaseController
{
    @Resource
    private ITbUserBaseInfoService tbUserBaseInfoService;

    /**
     * 查询用户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('user:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserReqDto dto)
    {
        startPage();
        List<TbUserBaseInfo> list = tbUserBaseInfoService.selectTbUserBaseInfoList(dto);
        return getDataTable(list);
    }

    /**
     * 导出用户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('user:info:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserReqDto dto)
    {
        List<TbUserBaseInfo> list = tbUserBaseInfoService.selectTbUserBaseInfoList(dto);
        ExcelUtil<TbUserBaseInfo> util = new ExcelUtil<>(TbUserBaseInfo.class);
        util.exportExcel(response, list, "用户基本信息数据");
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('user:info:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TbUserBaseInfo info)
    {
        return toAjax(tbUserBaseInfoService.updateStatus(info));
    }

    /**
     * 删除用户基本信息
     */
    @PreAuthorize("@ss.hasPermi('user:info:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        return toAjax(tbUserBaseInfoService.deleteTbUserBaseInfoByUserIds(userIds));
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:info:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId, String entityId)
    {
        return success(tbUserBaseInfoService.getDetailInfo(userId, entityId));
    }

    /**
     * 生成临时密码
     */
    @PreAuthorize("@ss.hasPermi('user:info:tempPwd')")
    @GetMapping("generateTempPassword/{userId}")
    public AjaxResult generateTempPassword(@PathVariable("userId") String userId) {
        return AjaxResult.success(tbUserBaseInfoService.generateTempPassword(userId));
    }
}
